package com.bobi.tfca.ui.mainnavigation.user.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentMyActivityBinding
import com.bobi.tfca.network.response.ActivityResponse
import com.bobi.tfca.ui.adapter.ActivityAdapter
import com.bobi.tfca.ui.mainnavigation.home.activity.ActivityFragmentVM
import com.bobi.tfca.util.LogUtil

class MyActivityFragment : BaseFragment<FragmentMyActivityBinding, ActivityFragmentVM>(
    FragmentMyActivityBinding::inflate,
    ActivityFragmentVM::class.java,
    true
) {
    private lateinit var activityRV: RecyclerView
    private lateinit var noDataIV: ImageView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentMyActivityBinding,
        viewModel: ActivityFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 设置topPanel
        setTopPanelStyle(binding)

        // 初始化RV
        initRecyclerView(binding)

        noDataIV = binding.ivNoData

        // 监听viewmodel
        viewModel!!.apply {
            activities.observe(this@MyActivityFragment) {
                val adapter = activityRV.adapter as ActivityAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentMyActivityBinding) {
        val topPanel = binding.topPanel
        topPanel.leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        rightLayout.visibility = View.GONE
        topPanel.titleText.text = "我的活动"
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentMyActivityBinding
    ) {
        // 获取RecyclerView实例
        activityRV = binding.rvActivity

        // 获得所有活动
        val activityList : ArrayList<ActivityResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        activityRV.layoutManager = linearLayoutManager
        activityRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        val articleAdapter = ActivityAdapter(this, activityList)

        // 设置监听器
        articleAdapter.setOnItemClickListener(object : ActivityAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转详情
                publicViewModel!!.activity.value = articleAdapter.activityList[position]
                requireActivity().findNavController(R.id.app_nav)
                    .navigate(R.id.action_myActivityFragment_to_activityDetailsFragment)
            }
        })
        // 绑定Adapter
        activityRV.adapter = articleAdapter
    }

    override fun resumeFragment(viewModel: ActivityFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.findMyActivities(publicViewModel!!, this, noDataIV)
    }
}